import { Component, OnInit } from '@angular/core'
import { AcquirerNewBaseClass } from '../baseClass'
import { FormBuilder } from '@angular/forms'
import { AcquirerStoreService } from '../acquirer-store.service'
import { I18NService } from '@core'

@Component({
  selector: 'app-acquirer-new-acquirer-func',
  templateUrl: './acquirer-func.component.html',
})
export class AcquirerNewAcquirerFuncComponent extends AcquirerNewBaseClass implements OnInit {

  isSupportDualMessage = false

  constructor(
    public fb: FormBuilder,
    public acquirerStore: AcquirerStoreService,
    private i18n: I18NService,
  ) {
    super(fb, acquirerStore)
  }

  ngOnInit() {
    this.initValidateForm()
  }

  initValidateForm(): void {
    const { isSupportDualMessage } = this.acquirer.institution.common.feature
    this.validateForm = this.fb.group({
      isSupportDualMessage: [this.parseBoolToString(isSupportDualMessage), []]
    })
  }

  checkValid(): boolean {
    this.pushDataToStore()
    return true
  }

  pushDataToStore(): void {
    const { isSupportDualMessage } = this.validateForm.value
    this.acquirer.institution.common.feature.isSupportDualMessage = this.parseStringToBool(isSupportDualMessage)
    this.acquirerStore.updateValue(this.acquirer)
  }

  trans(key: string, params?: Object): string {
    return this.i18n.fanyi(key, params)
  }

  private parseBoolToString(isSupportDualMessage: boolean): string {
    return !!isSupportDualMessage ? '1' : '0'
  }

  private parseStringToBool(str: string): boolean {
    return !!(+str)
  }
}
